﻿using ApiManage.Models.MongoMappings.Sys;
using ApiManage.Models.RequestModels;
using ApiManage.Svc.Filter;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Bson;
using MongoDB.Driver;
using Rhonin.Common.Entity.Api;
using Rhonin.Common.Entity.Exception;
using Rhonin.Common.Entity.Page;
using Rhonin.Common.Extentions;
using System;
using System.Linq;
using System.Linq.Expressions;
using ApiManage.Service.Mongo.Sys;
using ApiManage.Applications.Mongo.Sys;

namespace ApiManage.Svc.Controllers.System
{
    /// <summary>
    /// 用户接口
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class SysUserController : BaseController
    {
        private readonly ISysUserService _sysUserService;
        private readonly ISysUserQueryService _sysUserQueryService;
        /// <summary>
        /// 
        /// </summary>
        public SysUserController(ISysUserService sysUserService,
                                 ISysUserQueryService sysUserQueryService)
        {
            _sysUserService = sysUserService;
            _sysUserQueryService = sysUserQueryService;
        }
        /// <summary>
        /// 分页获取管理用户
        /// </summary>
        /// <param name="name"></param>
        /// <param name="roleId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [Route("GetPage"), HttpGet, CheckLogin]
        public ApiResult<PageResult<SysUser>> GetPage(string name, string roleId, int pageIndex, int pageSize)
        {
            var param = new QueryPage
            {
                PageIndex = pageIndex,
                PageSize = pageSize
            };
            Expression<Func<SysUser, bool>> where = o => o.Mark != 9;
            where = where.AndIf(o => o.UserName.Contains(name), name.IsNotEmpty());
            where = where.AndIf(o => o.RoleId == roleId, roleId.IsNotEmpty());
            var list = _sysUserQueryService.GetList(where);
            var result = new PageResult<SysUser>
            {
                TotalCount = list.Count,
                Items = list.OrderByDescending(o => o.CreateTime).Skip(param.Skip).Take(param.Limit),
                PageParameter = param
            };
            return CreateResult(result);
        }
        /// <summary>
        /// 编辑管理用户
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Save"), HttpPost, CheckLogin]
        public ApiResult Save(SysUserExtend param)
        {
            if (string.IsNullOrEmpty(param.UserName))
                throw new ApiException("用户名称不能为空");
            if (param.Id.IsNullOrEmpty())
            {
                _sysUserService.Insert(new SysUser
                {
                    LoginName = param.LoginName,
                    LoginPwd = param.LoginPwd,
                    UserName = param.UserName,
                    RoleId = param.RoleId,
                    RoleName = param.RoleName,
                    HeadImg = param.HeadImg,
                    Mark = param.Mark,
                    Remark = param.Remark
                });
            }
            else
            {
                _sysUserService.Update(u => u.Id == new ObjectId(param.Id), Builders<SysUser>.Update
                    .Set("LoginName", param.LoginName)
                    .Set("UserName", param.UserName)
                    .Set("RoleId", param.RoleId)
                    .Set("RoleName", param.RoleName)
                    .Set("HeadImg", param.HeadImg)
                    .Set("Mark", param.Mark)
                    .Set("Remark", param.Remark));
            }
            return CreateSuccess();
        }
        /// <summary>
        /// 删除管理用户
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Remove"), HttpPost, CheckLogin]
        public ApiResult Remove(IdParam param)
        {
            try
            {
                _sysUserService.Delete(u => u.Id == new ObjectId(param.Id));
            }
            catch (Exception ex)
            {
                return CreateError(ex.Message);
            }
            return CreateSuccess();
        }
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("ResetPwd"), HttpPost, CheckLogin]
        public ApiResult ResetPwd(ResetPwd param)
        {
            try
            {
                _sysUserService.Update(u => u.Id == new ObjectId(param.Id), Builders<SysUser>.Update
                    .Set("LoginPwd", param.Pwd));
            }
            catch (Exception ex)
            {
                return CreateError(ex.Message);
            }
            return CreateSuccess();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("UpdatePwd"), HttpPost, CheckLogin]
        public ApiResult UpdatePwd(UpdatePwd param)
        {
            try
            {
                Expression<Func<SysUser, bool>> where = o => o.Id == new ObjectId(UserId);
                var user = _sysUserQueryService.Get(where);
                if (user != null && !user.LoginPwd.Equals(param.OldPwd))
                {
                    return CreateError("旧密码错误");
                }
                _sysUserService.Update(u => u.Id == new ObjectId(UserId), Builders<SysUser>.Update
                    .Set("LoginPwd", param.Pwd));
            }
            catch (Exception ex)
            {
                return CreateError(ex.Message);
            }
            return CreateSuccess();
        }
    }
}